package com.citi.bond.controller;

import com.citi.bond.domain.Bond;
import com.citi.bond.service.BondService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class BondController {
    @Autowired
    private BondService bondService;

    @RequestMapping("/addBond")
    public String addBond(){
        return "pages/bondAdd";
    }

    @RequestMapping("/saveBond")
    public String saveBond(@RequestParam("name") String name,
                           @RequestParam("bond") String bond,
                           @RequestParam("datetime") String datetime,
                           @RequestParam("amount") Integer amount,
                           Model model) throws ParseException {
        String format = "yyyy-MM-dd'T'HH:mm";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = sdf.parse(datetime);

        if( bondService.saveBond(name,bond,date,amount) == 0){
            return "错误";
        }else {
            return "pages/bondAdd";
        }
    }

    @RequestMapping("/showBond")
    public String showBond(Model model){
        List<Bond> list = bondService.findAllBonds();
        model.addAttribute("bondList",list);
        return "pages/bondShow";
    }

}
